﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Web.Mvc;
using RedSand.Site.Models;

namespace RedSand.Site.Controllers
{
    public class ProjectController : BaseController
    {
        
		#region admin manager
		public ActionResult Manager()
        {
            return View(new ManagerModel { Title = Translate("ProjectManager", "Project") });
        }

        [AjaxOrChildAction]
        public ActionResult List(/* parameter */)
        {
			// search
            return PartialView();
        }

        [AjaxOrChildAction]
        public ActionResult Delete(int[] id)
        {
            if (id != null)
            {
                int count = id.Count();
                foreach (int i in id)
                {
                    ProjectInfo info = RsDataManager.GetInfo<ProjectInfo>("ProjectGetInfo", "ProjectID", i);
                    if (RsDataManager.Delete("ProjectDelete", "ProjectID", i))
                    {
                        //DeleteFile(info.Image);
                        count--;
                    }
                }
                if (count > 0)
                {
                    AddMessage(string.Format(Translate("CannotDeleteAnyRecords", "Message"), count));
                }
                else
                {
                    AddMessage(Message.DeleteSuccess);
                }
            }
            return RedirectToAction("List");
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Create()
        {
            return PartialView();
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Create(ProjectInfo info)
        {
            // check propertise
            if (ModelState.IsValid)
            {
                // set value
                if (RsDataManager.Save("ProjectSave", info))
                {
                    AddMessage(Message.CreateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.CreateError);
                }
            }
            return AjaxModelError();
        }

        [AjaxOrChildAction, HttpGet]
        public ActionResult Edit(int id)
        {
            return PartialView(RsDataManager.GetInfo<NewsInfo>("ProjectGetInfo", "ProjectID", id));
        }

        [AjaxOrChildAction, HttpPost]
        public ActionResult Edit(ProjectInfo info)
        {
            // check propertise
            if (ModelState.IsValid)
            {
                // set value
                if (RsDataManager.Save("ProjectSave", info))
                {
                    AddMessage(Message.UpdateSuccess);
                    return RedirectToAction("List");
                }
                else
                {
                    return AjaxMessage(Message.UpdateError);
                }
            }
            return AjaxModelError();
        }

        public JsonResult Gadget_List(byte status = 1, int pageIndex = 1, int pageSize = int.MaxValue)
        {
            int TotalRecord = 0;
            var List = RsDataManager.GetList<ProjectInfo>("ProjectSearch", new { Status = status }, pageIndex, pageSize, ref TotalRecord);
            return Json(new { List = List, TotalRecord = TotalRecord }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_ProjectJoinList(string Employee)
        {
            return Json(RsDataManager.GetList<ProjectInfo>("ProjectGetJoinList", new { Employee = Employee }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_JoinList(string Employee, byte? status)
        {
            return Json(RsDataManager.GetList<ProjectJoinInfo>("ProjectJoinGetList", new { Employee = Employee, Status = status}), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_JoinLeaveList(int ProjectID)
        {
            return Json(RsDataManager.GetList<ProjectJoinLeaveInfo>("ProjectGetJoinLeaveList", new { ProjectID = ProjectID }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_ReportList(string Employee, int? ProjectID, int pageIndex = 1, int pageSize = int.MaxValue)
        {
            int TotalRecord = 0;
            var List = RsDataManager.GetList<ProjectReportInfo2>("ProjectReportGetList", new { Employee = Employee, ProjectID = ProjectID }, pageIndex, pageSize, ref TotalRecord);
            return Json(new { List = List, TotalRecord = TotalRecord }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_JoinNameList(int ProjectID)
        {
            return Json(RsDataManager.GetList<ProjectJoinName>("ProjectGetJoinName", new { ProjectID = ProjectID }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_DeleteReport(int id)
        {
            return Json(RsDataManager.ExecuteGetReturn("ProjectReportDelete", new { ReportID = id }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_DeleteProject(int id)
        {
            return Json(RsDataManager.ExecuteGetReturn("ProjectDelete", new { ProjectID = id }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_Complete(int ProjectID, byte Status, string CompleteNote)
        {
            CompleteNote = string.IsNullOrWhiteSpace(CompleteNote) ? null : CompleteNote;
            return Json(RsDataManager.ExecuteGetReturn("ProjectComplete", new { ProjectID = ProjectID, Status = Status, CompleteNote = CompleteNote }), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_Join(ProjectJoinInfo info)
        {
            if (ModelState.IsValid)
            {
                info.JoinDate = DateTime.Now;
                return Json(RsDataManager.Save("ProjectJoinSave", info), JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_Leave(ProjectJoinInfo info)
        {
            if (ModelState.IsValid)
            {
                info.LeaveDate = DateTime.Now;
                return Json(RsDataManager.Save("ProjectJoinSave", info), JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
        public JsonResult Gadget_Report(ProjectReportInfo info)
        {
            if (ModelState.IsValid)
            {
                info.CreateDate = DateTime.Now;
                return Json(RsDataManager.Save("ProjectReportSave", info), JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }

        public JsonResult Gadget_Post(ProjectInfo info, float MaxTime)
        {
            if (ModelState.IsValid)
            {
                if (info.ProjectID < 1)
                {
                    info.Status = 1;
                    info.OrderDate = DateTime.Now;
                    info.Deadline = DateTime.Now.AddHours(MaxTime);
                }
                else
                {
                    info.OrderDate = RsDataManager.GetInfo<ProjectInfo>("ProjectGetInfo", "ProjectID", info.ProjectID).OrderDate;
                    info.Deadline = info.OrderDate.Value.AddHours(MaxTime);
                }
                return Json(RsDataManager.Save("ProjectSave", info), JsonRequestBehavior.AllowGet);
            }
            return Json(false, JsonRequestBehavior.AllowGet);
        }
		#endregion
    }
}
